package com.mybatis.generator.codegen;

import com.mybatis.generator.config.CodeGeneratorConfig;
import com.mybatis.generator.utils.FileUtil;
import com.mybatis.generator.utils.GenUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.IOException;
import java.util.Map;

public class CodeGeneratorController {

    private Configuration cfg;
    private CodeGeneratorConfig config;
    private String className;

    public CodeGeneratorController(Configuration cfg, CodeGeneratorConfig config, String className) {
        this.cfg = cfg;
        this.config = config;
        this.className = className;
    }

    public void build(Map<String, Object> variables, boolean isOverwrite) throws IOException {
        variables.put("packageController", config.getClassPackageController());
        variables.put("classNameControllerSuffix", config.getClassNameControllerSuffix());

        Template templateController = cfg.getTemplate(config.getTemplateFileNameController());
        FileUtil.buildTemplate(variables, config.getClassPathController(), config.getClassNameControllerPrefix() + GenUtil.firstCharacterToUpper(className) + config.getClassNameControllerSuffix() + ".java", templateController, isOverwrite);
    }
}
